﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BotonJXC.Entity;
using BotonJXC.BLL;
using BotonJXC.DAO;
using BotonJXC.Util;
using NHibernate;
using ComponentFactory.Krypton.Toolkit;

namespace BotonJXC.Forms.Controls
{
    public partial class InPayItemControl : BaseInfoControl
    {
        InPayItemBLL _inPayItemBll = new InPayItemBLL();
        string _itemName;
        string _itemCode;
        short _itemType;
        IList<InPayItem> _itemDatas;
        public InPayItemControl()
        {
            InitializeComponent();
        }

        private void InPayItemControl_Load(object sender, EventArgs e)
        {
            if (!this.DesignMode)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            PageModel<InPayItem> pageModel = _inPayItemBll.Find(_itemName, _itemCode, _itemType, pager1.PageStart, pager1.PageSize);
            pager1.TotalCount = pageModel.TotalCount;
            _itemDatas = pageModel.Results;
            kdgvInPayItem.DataSource = new BindingList<InPayItem>(_itemDatas);
        }

        private void kbSearch_Click(object sender, EventArgs e)
        {
            _itemName = ktbItemName.Text.Trim();
            _itemCode = ktbItemCode.Text.Trim();
            _itemType = (short)(kcbItemType.SelectedIndex + 1);
        }

        private void kdgvInPayItem_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 2 && _itemDatas.Count > e.RowIndex)
            {
                e.Value = _itemDatas[e.RowIndex].ItemType == 1 ? "收入" : "支出";
            }
        }

        public override IList<object> SelectedItems
        {
            get
            {
                IList<object> selectedItems = new List<object>();
                foreach (DataGridViewRow r in kdgvInPayItem.SelectedRows)
                {
                    selectedItems.Add(r.DataBoundItem);
                }
                return selectedItems;
            }
        }

        public override void AddInfo()
        {
            base.AddInfo();
        }

        public override void DeleteInfo()
        {
            IList<InPayItem> units = (IList<InPayItem>)kdgvInPayItem.DataSource;
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {
                foreach (DataGridViewRow r in kdgvInPayItem.SelectedRows)
                {
                    InPayItem item = r.DataBoundItem as InPayItem;
                    if (item != null)
                    {
                        item.Enabled = "N";
                        _inPayItemBll.Update(item);
                    }
                    units.RemoveAt(r.Index);
                }
                trans.Commit();
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
            }
            finally
            {
                HBHelper.Close();
            }
            kdgvInPayItem.Refresh();
        }
        public override void SaveInfo()
        {
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {
                foreach (DataGridViewRow r in kdgvInPayItem.Rows)
                {
                    InPayItem u = r.DataBoundItem as InPayItem;
                    if (u == null)
                        continue;
                    if (u.InPayItemId == 0)
                    {
                        u.Enabled = "Y";
                        _inPayItemBll.Save(u);
                    }
                    else if ("1".Equals(r.Tag) && u.InPayItemId != 0)
                    {
                        _inPayItemBll.Update(u);
                    }
                }
                trans.Commit();
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
            }
            finally
            {
                HBHelper.Close();
            }
        }

        private void kdgvInPayItem_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
                kdgvInPayItem.Rows[e.RowIndex].Tag = "1";
        }

        private void kdgvInPayItem_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (kdgvInPayItem.CurrentCell.ColumnIndex == 2)
            {
                KryptonDataGridViewComboBoxEditingControl cbc = e.Control as KryptonDataGridViewComboBoxEditingControl;
                cbc.DropDownClosed += new EventHandler(cbc_DropDownClosed);
            }
        }

        void cbc_DropDownClosed(object sender, EventArgs e)
        {
            KryptonDataGridViewComboBoxEditingControl cbc = sender as KryptonDataGridViewComboBoxEditingControl;
            IList<InPayItem> itemDatas = (IList<InPayItem>)kdgvInPayItem.DataSource;
            itemDatas[kdgvInPayItem.CurrentCell.RowIndex].ItemType = (short)(cbc.SelectedIndex + 1);
        }
    }
}
